package com.source.savings;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.source.baseData.entity.*;
import com.source.baseData.service.*;
import com.source.baseData.vo.PoultryCategoryVo;
import com.source.baseData.vo.ToMarketVO;
import com.source.labelAndBarcode.service.LabelingRecordService;
import com.source.labelAndBarcode.vo.SlaughterHouseVo;
import com.source.labelAndBarcode.vo.StallsVo;
import com.source.sys.entity.User;
import com.source.sys.service.UserService;
import com.source.utils.CommonResult;
import com.source.utils.ImageCatalogueEnum;
import com.source.utils.ImageUtils;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author Joey
 * @since 2021-03-12
 */

@Api(tags = "save类型的接口专用的查询")
@RestController
@CrossOrigin
@RequestMapping("/saving")
public class Saving {

    @Autowired
    LabelingRecordService recordService;

    @Autowired
    SlaughterHouseService houseService;

    @Autowired
    StallsService stallsService;

    @Autowired
    PoultryCategoryParentService parentService;

    @Autowired
    PoultryCategoryService categoryService;

    @Autowired
    BarcodeMachineService machineService;

    @Autowired
    UserService userService;

    @Autowired
    DistrictService districtService;

    @Autowired
    ToMarketService marketService;


    @ApiOperation(value = "查询所有脚标机")
    @GetMapping("/findAllBM")
    public CommonResult<List<BarcodeMachine>> findAllBM() {
        List<BarcodeMachine> list = machineService.list();
        return new CommonResult<List<BarcodeMachine>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "查询所有合法档口")
    @GetMapping("/findAllStalls")
    public CommonResult<List<StallsVo>> findAllStalls() {
        List<StallsVo> list = stallsService.findAllOnMultiStatus(1);
        return new CommonResult<List<StallsVo>>(ResultEnum.SUCCESS, list);
    }


    @ApiOperation(value = "查询所有合法去向市场")
    @GetMapping("/findAllToMarket")
    public CommonResult<List<ToMarketVO>> findAllToMarket() {
        List<ToMarketVO> list = marketService.find(1);
        return new CommonResult<List<ToMarketVO>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "查询所有合法屠宰场")
    @GetMapping("/findAllSH")
    public CommonResult<List<SlaughterHouseVo>> findAllSH() {
        List<SlaughterHouseVo> list = houseService.findAllOnPermission();
        return new CommonResult<List<SlaughterHouseVo>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "查询所有合法父级禽类")
    @GetMapping("/findAllPCP")
    public CommonResult<List<PoultryCategoryParent>> findAllPCP() {
        QueryWrapper<PoultryCategoryParent> wrapper = new QueryWrapper<>();
        wrapper.eq("isDelete",0);
        List<PoultryCategoryParent> list = parentService.list(wrapper);
        return new CommonResult<List<PoultryCategoryParent>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "查询所有合法子类禽类")
    @GetMapping("/findAllPoultryCategory")
    public CommonResult<List<PoultryCategory>> findAllPoultryCategory() {
        QueryWrapper<PoultryCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("isDelete", 0);
        List<PoultryCategory> list = categoryService.list(wrapper);
        return new CommonResult<List<PoultryCategory>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "查询所有合法用户")
    @GetMapping("/findAllUsers")
    public CommonResult<List<User>> findAllUsers() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("isDelete",0);
        List<User> list = userService.list(wrapper);
        return new CommonResult<List<User>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "查询所有合法的屠宰场维修人员")
    @GetMapping("/findAllRepairs")
    public CommonResult<List<User>> findAllRepairs(){
        List<User> list = userService.findByRoleId("1234");
        return new CommonResult<List<User>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "查找所有省、直辖市")
    @GetMapping("/findAllProvinces")
    public CommonResult<List<District>> findAllProvinces() {
        QueryWrapper<District> wrapper = new QueryWrapper<>();
        List<District> list = districtService.list(wrapper.eq("Pid",0).ne("id",0));
        return new CommonResult<List<District>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "查找省级下的市")
    @ApiImplicitParam(name = "id",value = "选择了省后，返回的主键id")
    @GetMapping("/findCities")
    public CommonResult<List<District>> findCities(@RequestParam Integer id) {
        QueryWrapper<District> wrapper = new QueryWrapper<>();
        List<District> list = districtService.list(wrapper.eq("Pid",id));
        return new CommonResult<List<District>>(ResultEnum.SUCCESS,list);
    }

    @ApiImplicitParam(name = "id",value = "选择了市后，返回的主键id")
    @ApiOperation(value = "查找市下的区县")
    @GetMapping("/findDistricts")
    public CommonResult<List<District>> findDistricts(@RequestParam Integer id) {
        QueryWrapper<District> wrapper = new QueryWrapper<>();
        List<District> list = districtService.list(wrapper.eq("Pid",id));
        return new CommonResult<List<District>>(ResultEnum.SUCCESS,list);
    }

    @Deprecated
    @ApiOperation(value = "按省市区返回所有市场")
    @GetMapping("/findByProvinceAndCityAndDistrict/{province}/{city}/{district}")
    public CommonResult<List<SlaughterHouse>> findByProvinceAndCityAndDistrict(@PathVariable(value = "province") String province,
                                                                               @PathVariable(value = "city") String city,
                                                                               @PathVariable(value = "district") String district) {
        QueryWrapper<SlaughterHouse> wrapper = new QueryWrapper<SlaughterHouse>();
        wrapper.eq("province", province).and(i -> i.eq("city", city).eq("district", district));
        List<SlaughterHouse> list = houseService.list(wrapper);
        return new CommonResult<List<SlaughterHouse>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), list);
    }

    @ApiOperation(value = "根据区来返回所有屠宰场")
    @GetMapping("/findHouseByDistrictId/{id}")
    public CommonResult<List<SlaughterHouseVo>> findHouseByDistrictId(@PathVariable String id){
        District byId = districtService.getById(id);
        List<SlaughterHouseVo> list = houseService.findByDistrict(byId.getName());
        return new CommonResult<List<SlaughterHouseVo>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据区来返回所有去向市场")
    @GetMapping("/findMarketByDistrictId/{id}")
    public CommonResult<List<ToMarketVO>> findMarketByDistrictId(@PathVariable String id){
        District byId = districtService.getById(id);
        List<ToMarketVO> list = marketService.findMarketByDistrictId(byId.getName());
        return new CommonResult<List<ToMarketVO>>(ResultEnum.SUCCESS,list);
    }

    /*@ApiOperation(value = "按省返回所有市")
    @GetMapping("/findCitiesByProvince/{province}")
    public CommonResult<List<SlaughterHouse>> findCitiesByProvince(@PathVariable(value = "province") String province) {
        QueryWrapper<SlaughterHouse> wrapper = new QueryWrapper<SlaughterHouse>();
        List<SlaughterHouse> list = houseService.list(wrapper.select("city").eq("province", province));
        return new CommonResult<List<SlaughterHouse>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), list);
    }

    @ApiOperation(value = "按省市返回所有区")
    @GetMapping("/findDistrictsByProvinceAndCity/{province}/{city}")
    public CommonResult<List<SlaughterHouse>> findDistrictsByProvinceAndCity(@PathVariable(value = "province") String province, @PathVariable(value = "city") String city) {
        QueryWrapper<SlaughterHouse> wrapper = new QueryWrapper<SlaughterHouse>();
        wrapper.select("district").eq("province", province).and(i -> i.eq("city", city));
        List<SlaughterHouse> list = houseService.list(wrapper);
        return new CommonResult<List<SlaughterHouse>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), list);
    }*/

    @PostMapping("/saveImage")
    @ApiOperation(value = "图片上传,注意真正保存的路径是：user\\lightfowl\\返回的路径(image\\imageCatalogueEnum.目录名\\图片名（前端不用管他）)",
            notes = "图片上传接口，输入参数有multipartFile，imagePath 都不是必要的元素（可不写）," +
            "<br>imagePath 不为空就会删除原来的image的图片，再保存新的图片，用于更新图片是使用" +
            "<br>imagePath 为空，就会直接保存图片，用于保存数据时使用" +
            "<br>枚举类说明根据模块的需要进行选择，nums必须选(0——5)" +
            "<br>0 表示  BarcodeMachine_Catalogue;\n" +
            "<br>1 表示  SlaughterHouse_Catalogue;\n" +
            "<br>2 表示  User_Catalogue;\n" +
            "<br>3 表示  Stalls_Catalogue;\n" +
            "<br>4 表示  ToMarketMarkCode_Catalogue;\n" +
            "<br>5 表示  ToMarketStallCode_Catalogue;\n" +
            "<br>6 表示  BarcodeMachineRepair;\n" +
            "<br> 例如如果需要访问图片的路径为 http://8.129.53.188:9000/image/BarcodeMachine/1c8a615a.jpg")
    public CommonResult saveImage(@RequestPart(value = "multipartFile",required = false) MultipartFile multipartFile,
                                          @RequestParam(value = "imagePath",required = false) String imagePath,
                                          @RequestParam("nums") Integer nums) throws Exception {
        ImageCatalogueEnum imageCatalogueEnum = ImageUtils.selectCatalogue(nums);
        if (multipartFile.isEmpty()){
            return CommonResult.error("图片不能为空");
        }else if (imageCatalogueEnum.getMsg()==null){
            return CommonResult.error("nums参数出错");
        }
        else {
            String image = ImageUtils.RemoveAndSaveImage(multipartFile,imagePath,imageCatalogueEnum);
            if (image.isEmpty()){
                return CommonResult.error("图片路径为空");
            }else {
                return  CommonResult.success("添加成功").data(image);
            }
        }
    }

    @DeleteMapping("/deleteImage")
    @ApiOperation(value = "删除图片",
            notes = "直接填写路径就行，例如：/BarcodeMachine/7baf3629.jfif")
    public CommonResult<String> deleteImage(@RequestParam("imagePath") String imagePath){
        if (imagePath.isEmpty()){
            return new CommonResult<String>(ResultEnum.PARAMS_NULL.getCode(),ResultEnum.PARAMS_NULL.getMsg(),null);
        }else {
            ImageUtils.delete(imagePath);
            return new CommonResult<String>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),imagePath);
         }
    }


}
